
package com.hyst.common;

import lombok.Data;

/**
 * @Description: 结果返回
 * @Date：2017年12月6日 @author： DongY
 * @version ：1.0 1.[2017年12月6日]CreatBy：dy
 */
@Data
public class ResultBean<T> implements ResultAble<T> {

	private static final long serialVersionUID = 1L;

	private int code = SUCCESS;

	private String msg = "success";

	private T data;

	/**
	 * @Description: 请求成功返回结果，采用默认消息
	 * @param data
	 * @return ResultBean<T>
	 */
	public static <T> ResultBean<T> success(final T data) {
		return new ResultBean<>(data);
	}

	/**
	 * @Description: 请求成功返回结果，自定义返回消息
	 * @param data
	 * @param msg
	 * @return ResultBean<T>
	 */
	public static <T> ResultBean<T> success(final T data, final String msg) {
		return new ResultBean<>(data, msg);
	}
	
	public static <T> ResultBean<T> success(final String msg, final int code) {
		return new ResultBean<>(msg, code);
	}

	/**
	 * @Description: 请求失败时返回数据，根据异常信息返回描述信息
	 * @param e
	 * @return ResultBean<Object>
	 */
	public static ResultBean<?> fail(final Throwable e) {
		return new ResultBean<>(e.toString(), ResultBean.FAIL);
	}

	/**
	 * @Description: 请求失败时返回数据，自定义错误信息
	 * @param e
	 * @return ResultBean<Object>
	 */
	public static <T> ResultBean<T> fail(final String msg) {
		return new ResultBean<>(msg, ResultBean.FAIL);
	}

	/**
	 * @Description: 不具备访问权限
	 * @return ResultBean<Object>
	 */
	public static ResultBean<?> noPermisson() {
		return new ResultBean<>("不具备该访问权限！", ResultBean.NO_PERMISSION);
	}

	/**
	 * @Description:用户未登录
	 * @return ResultBean<Object>
	 */
	public static ResultBean<?> noLogin() {
		return new ResultBean<>("用户未登录！", ResultBean.NO_LOGIN);
	}

	private ResultBean(final T data) {
		this.data = data;
	}

	private ResultBean(final T data, final String msg) {
		this.msg = msg;
		this.data = data;
	}

	private ResultBean(final String msg, final int code) {
		this.msg = msg;
		this.code = code;
	}
}
